package com.jumi.microservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jumi.microservice.common.base.BaseController;
import com.jumi.microservice.common.base.dto.BasePageResponse;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.core.aspect.WebLog;
import com.jumi.microservice.common.utils.ResultGenerator;
import com.jumi.microservice.constant.NumberConstant;
import com.jumi.microservice.domain.Log;
import com.jumi.microservice.domain.LogDetailVO;
import com.jumi.microservice.domain.OperateLogQuery;
import com.jumi.microservice.domain.vo.LogVO;
import com.jumi.microservice.service.ILogService;
import com.jumi.microservice.service.LogService;
import com.jumi.microservice.utils.LogObjectCopyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 日志表 前端控制器
 * </p>
 *
 * @author liuz
 * @since 2020-07-18
 */
@RestController
@Api(tags = "日志表 相关接口")
public class LogController extends BaseController implements ILogService {

    @Autowired
    private LogService logService;

    @ApiOperation(value = "查询分页", notes = "获取日志表分页列表数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码"),
            @ApiImplicitParam(name = "size", value = "分页大小"),
    })
    @GetMapping("/list")
    public BasePageResponse list(
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "0") Integer size,
            LogVO vo) {
        LambdaQueryWrapper<Log> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(vo.getDepartmentId() != null, Log::getDepartmentId, vo.getDepartmentId());
        wrapper.like(StringUtils.isNotEmpty(vo.getUserName()), Log::getUserName, vo.getUserName());
        wrapper.like(StringUtils.isNotEmpty(vo.getMobile()), Log::getMobile, vo.getMobile());
        wrapper.like(StringUtils.isNotEmpty(vo.getPriorityName()), Log::getPriorityName, vo.getPriorityName());
        wrapper.eq(vo.getPriorityId() != null, Log::getPriorityId, vo.getPriorityId());
        wrapper.eq(vo.getOperateType() != null, Log::getOperateType, vo.getOperateType());
        wrapper.like(StringUtils.isNotEmpty(vo.getOperateAction()), Log::getOperateAction, vo.getOperateAction());
        wrapper.ge(StringUtils.isNotEmpty(vo.getStartTime()), Log::getOperateTime, vo.getStartTime());
        wrapper.le(StringUtils.isNotEmpty(vo.getEndTime()), Log::getOperateTime, vo.getEndTime());
        wrapper.like(StringUtils.isNotEmpty(vo.getDepartmentName()), Log::getDepartmentName, vo.getDepartmentName());
        wrapper.orderByDesc(Log::getOperateTime);
        IPage<Log> pageInfo = logService.page(
                new Page<>(page, size), wrapper);
        return ResultGenerator.genSuccessResultPage(pos2Vos(pageInfo.getRecords()), pageInfo.getTotal());
    }

    @ApiOperation(value = "一键回收/恢复", notes = "操作日志一键回收/恢复")
    @PostMapping("/recyclingOrRecovering")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "vo", value = "搜索条件"),
            @ApiImplicitParam(name = "type", value = "操作类型(0:一键恢复,1:一键回收)"),
    })
    @WebLog(description = "一键回收/恢复")
    public ResponseResult recyclingOrRecovering(LogVO vo, Integer type) {
        logService.recyclingOrRecovering(vo, type);
        return ResultGenerator.genSuccessResult();
    }

    /**
     * 列表对象转换 po->vo
     *
     * @param pos
     * @return
     */
    private List<LogVO> pos2Vos(List<Log> pos) {
        List<LogVO> vos = pos.stream().map(p -> {
            LogVO vo = null;
            try {
                vo = p.clone(LogVO.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return vo;
        }).collect(Collectors.toList());
        return vos;
    }

    /**
     * 操作日志分页查询
     *
     * @param operateLogQuery 操作查询条件
     * @return LogDetailVO集合
     */
    @Override
    @ApiOperation(value = "日志查询", notes = "日志查询")
    public BasePageResponse<List<LogDetailVO>> listOperateLogs(@RequestBody OperateLogQuery operateLogQuery) {
        Page<Log> logDetails = logService.listOperateLogs(operateLogQuery);

        return ResultGenerator.genSuccessResultPage(LogObjectCopyUtil.convertOperateLogDoList2VoList(logDetails.getRecords()),logDetails.getTotal());
    }
}
